package com.jinzecloud.reportapproval.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinzecloud.reportapproval.bean.SoliChild;
import com.jinzecloud.reportapproval.bean.SpeedOperationTable;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import com.jinzecloud.reportapproval.service.SoliChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import com.jinzecloud.reportapproval.bean.SoilFirmnessMoisture;
import com.jinzecloud.reportapproval.service.SoilFirmnessMoistureService;
import com.jinzecloud.reportapproval.utils.PageUtils;
import com.jinzecloud.reportapproval.utils.R;



/**
 * 表7 土壤坚实度、土壤含水率测定记录表
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-04 15:46:42
 */
@RestController
@RequestMapping("reportapproval/soilfirmnessmoisture")
public class SoilFirmnessMoistureController {
    @Autowired
    private SoilFirmnessMoistureService soilFirmnessMoistureService;

    @Autowired
    private SoliChildService soliChildService;

    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:soilfirmnessmoisture:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = soilFirmnessMoistureService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:soilfirmnessmoisture:info")
    public R info(@PathVariable("id") Integer id){
		SoilFirmnessMoisture soilFirmnessMoisture = soilFirmnessMoistureService.getById(id);

        List<SoliChild> list = soliChildService.list(new QueryWrapper<SoliChild>().eq("parent_id", id).orderByAsc("id"));
        soilFirmnessMoisture.setSoliChilds(list);

        return R.ok().put("soilFirmnessMoisture", soilFirmnessMoisture);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:soilfirmnessmoisture:save")
    public R save(@RequestBody SoilFirmnessMoisture soilFirmnessMoisture){
        List<SoilFirmnessMoisture> projectCode = soilFirmnessMoistureService.list(new QueryWrapper<SoilFirmnessMoisture>().eq("project_code", soilFirmnessMoisture.getProjectCode()));
        if (projectCode.size() > 0) return R.error("编号："+soilFirmnessMoisture.getProjectCode()+"已存在，请勿重复添加！");
        soilFirmnessMoisture.setStatus(StateEnum.UN_SUBMIT.getState()).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        soilFirmnessMoisture.setUserId(adminService.getIdByName(authentication.getName()));
        return  soilFirmnessMoistureService.saveSoilFirmnessMoisture(soilFirmnessMoisture);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:soilfirmnessmoisture:update")
    public R update(@RequestBody SoilFirmnessMoisture soilFirmnessMoisture){
		return  soilFirmnessMoistureService.updateSoilFirmnessMoisture(soilFirmnessMoisture);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("reportapproval:soilfirmnessmoisture:delete")
    public R delete(@RequestBody Integer[] ids){
		soilFirmnessMoistureService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id){
        return soilFirmnessMoistureService.viewPdf(id);
    }

    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(soilFirmnessMoistureService.auditList(param));
    }

    @PostMapping("/review")
    public R review(@RequestBody SoilFirmnessMoisture soilFirmnessMoisture) {
        soilFirmnessMoistureService.updateById(new SoilFirmnessMoisture().setId(soilFirmnessMoisture.getId()).setStatus(0));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return soilFirmnessMoistureService.audit(auditParam);
    }

}
